package android.crosstime.hes;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.android.hes.R;

public class BaseFileListAdapter extends BaseAdapter {

	protected File mCurrentDir = new File("/");
	protected File mOldDir = new File("/");
	protected LayoutInflater mInflater 	 = null;
	protected ArrayList<ListItem> mFileList = new ArrayList<ListItem>();
	protected Context mContext = null;
	
	public BaseFileListAdapter(Context context){
		mInflater = LayoutInflater.from(context);
		mContext = context;
		reloadList();
		sortByNameOrder();
	}
	
	@Override
	public int getCount() {
		return mFileList.size();
	}
	@Override
	public Object getItem(int position) {
		return mFileList.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null)
		{
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.img_view);
			holder.text = (TextView) convertView.findViewById(R.id.text_view);
			holder.listBtn = (ImageView) convertView.findViewById(R.id.img_right);			
			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder) convertView.getTag();
		ListItem li = mFileList.get(position);
		String text = li.Name;
		holder.text.setText(text);
		int icon = li.IconId;
		holder.icon.setImageResource(icon);
		holder.listBtn.setVisibility(View.GONE);
		return convertView;
	}

	public String getFilePathByPosition(int position){
		if(position >= mFileList.size())
			return mCurrentDir.getAbsolutePath();

		return mFileList.get(position).FullPath;
	}
	
	public boolean changeDirectory(String strPath){
		File dir = new File(strPath);
		if(dir.exists() && dir.isDirectory())
		{
			mOldDir = mCurrentDir;
			mCurrentDir = dir;
			if(!reloadList())
			{
				mCurrentDir = mOldDir;
				return false;
			}
			else
				notifyDataSetChanged();
			return true;
		}else
			return false;
	}
	
	public boolean openParentFolder(){
		String parent = mCurrentDir.getParent();
		if(parent == null)
			return false;
		return changeDirectory(parent);
	}
	
	protected boolean reloadList(){
		File[] fileList = mCurrentDir.listFiles();
		if(fileList == null)
			return false;
		mFileList.clear();
		if(mCurrentDir.getParent() != null)
			mFileList.add(new ListItem(mCurrentDir.getParentFile(), ListItemUtil.TYPE_UPLEVEL));
		for(File file : fileList){
			mFileList.add(new ListItem(file));
		}
		sortByNameOrder();
		return true;
	}

	protected void sortByNameOrder(){
		Collections.sort(mFileList, new Comparator<ListItem>() {

			@Override
			public int compare(ListItem obj1, ListItem obj2) {

				if(obj1 != null && obj2 != null)
				{
					boolean isDir1 = obj1.isDirectory(); 
					boolean isDir2 = obj2.isDirectory();
					
					if(obj1.Name.equals(".."))
						return -1;
					if(obj2.Name.equals(".."))
						return 1;

					if(isDir1 && !isDir2)
						return -1; //obj1 ahead of obj2
					else if(!isDir1 && isDir2)
						return 1;
					return obj1.Name.compareTo(obj2.Name);  
				}
				return 0;
			} });  
	}
	
	protected class ViewHolder{
		public ImageView icon;
		public TextView text;
		public ImageView listBtn;
	}

}
